package com.macro.mall.controller;

import com.macro.mall.annotationUtil.CurrentUserName;
import com.macro.mall.common.api.CommonPage;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.exception.ApiException;
import com.macro.mall.dto.*;
import com.macro.mall.mapper.OmsOrderSettingMapper;
import com.macro.mall.model.*;
import com.macro.mall.service.UmsAdminService;
import com.macro.mall.service.UmsStoreService;
import com.macro.mall.wx.service.GetQrCodeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 后台用户店铺管理
 * Created by macro on 2018/9/30.
 */
@Controller
@Api(tags = "UmsStoreController", description = "后台用户店铺管理")
@RequestMapping("/store")
public class UmsStoreController {

    @Autowired
    private UmsStoreService storeService;
    @Autowired
    private GetQrCodeService getQrCodeService;
    @Autowired
    private UmsAdminService umsAdminService;
    @Autowired
    private OmsOrderSettingMapper settingMapper;

    /**
     * 查询店铺信息
     *
     * @param keyWord  账户名称
     * @param pageSize
     * @param pageNum
     * @return
     * @author 高强
     * @Date 2020年8月25日
     */
    @ResponseBody
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<CommonPage<UmsStoreDTO>> getStoreList(@CurrentUserName String username,
                                                              @RequestBody FilterParam param) {
        List<UmsStoreDTO> list = storeService.getStoreByUsername(username, param.getKeyWord(), param.getPageSize(), param.getPageNum());
        return CommonResult.success(CommonPage.restPage(list));
    }

//    public CommonResult<CommonPage<UmsStoreDTO>> getStoreList(@CurrentUserName String username,
//                                                              @RequestParam(value = "keyword", required = false) String keyWord,
//                                                              @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
//                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
//        List<UmsStoreDTO> list = storeService.getStoreByUsername(username, keyWord, pageSize, pageNum);
//        return CommonResult.success(CommonPage.restPage(list));
//    }

    /**
     * 增加用户和店铺
     *
     * @param param 用户和店铺的dto
     * @return 1为增加成功
     * @author 高强
     * @Date 2020年8月25日
     */
    @ResponseBody
    @RequestMapping(value = "/creatStore", method = RequestMethod.POST)
    public CommonResult creatStore(@RequestBody UmsAdminAndStoreParam param) {
        try {
            int count = storeService.create(param);
            if (count > 0) {
                return CommonResult.success(count);
            }
            return CommonResult.failed();
        } catch (ApiException api) {
            return CommonResult.failed(api.getMessage());
        }
    }

    /**
     * 修改店铺信息
     *
     * @param id    需要更改的店铺id
     * @param param 需要更改的店铺信息
     * @return 1为修改成功
     * @author 高强
     * @Date 2020年8月25日
     */
    @ResponseBody
    @RequestMapping(value = "/updateStore/{id}", method = RequestMethod.POST)
    public CommonResult updateStore(@PathVariable(value = "id") Long id,
                                    @RequestBody UmsAdminAndStoreParam param) {
        int count = storeService.update(id, param);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 根据id查询店铺
     *
     * @param id
     * @return
     * @author 高强
     * @Date 2020年8月25日
     */
    @ResponseBody
    @RequestMapping(value = "/getStore/{id}", method = RequestMethod.GET)
    public CommonResult<Map<String, Object>> getStore(@CurrentUserName String userName,@PathVariable(value = "id") long id) {
        Map<String, Object> result = new HashMap<>();
        UmsAdmin admin = this.umsAdminService.getAdminByUsername(userName);
        UmsStore store = storeService.selectByPrimaryKey(id);
        result.put("store",store);
        result.put("admin",admin);
        return CommonResult.success(result);
    }

    /**
     * 更改店铺状态：1开启/0关闭
     *
     * @param id     店铺id
     * @param status 店铺状态
     * @return
     * @author 高强
     * @Date 2020年8月28日
     */
    @ApiOperation("修改店铺状态")
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateStatus(Long id, Integer status) {
        UmsAdminAndStoreParam umsStore = new UmsAdminAndStoreParam();
        umsStore.setStatus(status);
        int count = storeService.update(id, umsStore);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 更改店铺状态：1开启/0关闭
     *
     * @param id          店铺id
     * @param auditStatus 店铺是否需要审核 0->需要; 1->不需要
     * @return
     * @author 高强
     * @Date 2020年8月28日
     */
    @ApiOperation("修改店铺是否需要审核")
    @RequestMapping(value = "/updateAudit/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateAuditStatus(@PathVariable Long id, @RequestParam(value = "status") Integer auditStatus) {
        UmsAdminAndStoreParam umsStore = new UmsAdminAndStoreParam();
        umsStore.setAuditStatus(auditStatus);
        int count = storeService.update(id, umsStore);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * 门店管理员设置签到积分
     *
     * @param currentUserName
     * @param umsCheckInIntegration
     * @return
     * @author 高强
     * @Date 2020年9月7日
     */
    @RequestMapping(value = "/setCheckIn", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult setCheckInIntegration(@CurrentUserName String currentUserName, @RequestBody UmsCheckInIntegration umsCheckInIntegration) {
        return storeService.setCheckInIntegration(currentUserName, umsCheckInIntegration);
    }

    /**
     * 获取门店设置积分
     *
     * @param currentUserName
     * @return
     */
    @RequestMapping(value = "/getCheckIn", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<UmsCheckInIntegration> getCheckInIntegration(@CurrentUserName String currentUserName) {
        return storeService.getCheckInIntegration(currentUserName);
    }

    /**
     * 获取当前登录门店的所有买家
     *
     * @param currentUserName
     * @return
     * @author 高强
     * @Date 2020年9月8日
     */
    @RequestMapping(value = "/getMyBuyer", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<CommonPage<UmsMemberAndAgentPhone>> getMyBuyer(@CurrentUserName String currentUserName,
                                                                       @RequestBody UmsMemberDto member) {
        CommonPage<UmsMemberAndAgentPhone> myBuyer = storeService.getMyBuyer(currentUserName, member, member.getPageSize(), member.getPageNum());
        return CommonResult.success(myBuyer);
    }
    /**
     * 获取所有的省级代理用户
     *
     * @return
     * @author zhouboyu
     * @Date 2020年10月3日
     */
    @RequestMapping(value = "/getProvincialAdmins", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<UmsAdmin>> getProvincialAdmins() {
        List<UmsAdmin> list = this.umsAdminService.getProvincialAdmins();
        return CommonResult.success(list);
    }
    /**
     * 订单商品统计
     *
     * @param param 查询条件
     * @return
     */
    @RequestMapping(value = "/getProductStat", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getOrderProductStat(@CurrentUserName String username, @RequestBody OrderProductStatParam param) {
        return storeService.getProductStat(username, param);
    }

    /**
     * 设置会员权益
     *
     * @param currentUserName
     * @param memberEquity    会员权益
     * @return
     * @author 高强
     * @Date 2020年9月13日
     */
    @RequestMapping(value = "/setMemberEquity", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult setMemberEquity(@CurrentUserName String currentUserName, @RequestBody UmsStore store) {
        return storeService.setMemberEquity(currentUserName, store);
    }

    /**
     * 获取会员权益
     *
     * @param currentUserName
     * @param memberEquity    会员权益
     * @return
     * @author 高强
     * @Date 2020年9月13日
     */
    @RequestMapping(value = "/getMemberEquity", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult getMemberEquity(@CurrentUserName String currentUserName) {
        return storeService.getMemberEquity(currentUserName);
    }



    /**
     * 获取二维码
     *
     * @param currentUserName
     * @return
     * @author zhouboyu
     * @Date 2020年9月13日
     */
    @GetMapping(value = "/getQrCode")
    @ResponseBody
    public CommonResult<String> getQrCode(HttpServletResponse response, @CurrentUserName String currentUserName,String page) {
        UmsAdmin adminByUsername = this.umsAdminService.getAdminByUsername(currentUserName);
        OmsOrderSetting omsOrderSetting = this.settingMapper.selectByPrimaryKey(1L);
        byte[] qrCode = this.getQrCodeService.getQrCode(adminByUsername.getId().toString(), page, omsOrderSetting.getAccessToken());
        return CommonResult.success((new sun.misc.BASE64Encoder()).encode(qrCode));
    }
}
